package cn.com.ddapp.ddm.fragment.base.limittime;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.NewGoodsAdapter;
import cn.com.ddapp.ddm.adapter.NewGoodsOutAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.ActivityBean;
import cn.com.ddapp.ddm.fragment.base.BaseFragment;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.CommidContentActivity;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.CountDownLimitTimer;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.TLog;

/**
 * Created by Administrator on 2016/8/23 0023.
 */
public class TimeFiveFragment extends BaseFragment {
    @BindView(R.id.definiteList)
    ListView definiteList;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.limittime)
    CountDownLimitTimer limittime;
    @BindView(R.id.limit_ing)
    TextView limit_ing;
    @BindView(R.id.limit_out)
    TextView limit_out;
    @BindView(R.id.img_error_layout)
    ImageView img;
    private ActivityBean activityBean;
    private List<ActivityBean.PromotionListBean> promotionListBeen;
    private NewGoodsOutAdapter outAdapter;
    private NewGoodsAdapter newGoodsAdapter;
    SimpleDateFormat hdf=new SimpleDateFormat("HH");
    SimpleDateFormat mdf=new SimpleDateFormat("mm");
    String hTime=hdf.format(new Date());
    String mTime=hdf.format(new Date());
    private int h,m;
    @Override
    protected String getFragmentTag() {
        return null;
    }

    @Override
    protected void initData() {
        if (!NetUtils.isConnected(getContext())) {
            Toast.makeText(getContext(), "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
                    TLog.log(getFragmentTag(), response);
                    Log.e("TAG", "onMyResponse: " + response);
                    activityBean = gson.fromJson(response, ActivityBean.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(activityBean.getResult())) && activityBean.getLogin()) {
                        mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(activityBean.getResult()))) {
                        BaseApplication.getIntstance().showToast(activityBean.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    } else if (activityBean.getResult().equals("OK") && activityBean.getPromotionList().size() == 0) {
                        mEmptyLayout.setErrorType(EmptyLayout.NODATA);
                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(activityBean.getResult()))) {

                        promotionListBeen = activityBean.getPromotionList();
                        if(AppConfig.RESULTFIVE.equals("已结束")){
                            outAdapter=new NewGoodsOutAdapter(getContext(), promotionListBeen);
                            definiteList.setAdapter(outAdapter);
                            limit_out.setVisibility(View.VISIBLE);
                            limit_ing.setVisibility(View.GONE);
                            limittime.setTime(0,0,0);
                            limittime.start();
                        }else  if(AppConfig.RESULTFIVE.equals("未开抢")){
                            outAdapter=new NewGoodsOutAdapter(getContext(), promotionListBeen);
                            definiteList.setAdapter(outAdapter);
                            limit_out.setVisibility(View.GONE);
                            limit_ing.setVisibility(View.VISIBLE);
//                            String  str = theTime.substring(0, theTime.indexOf(":")) + theTime.substring(theTime.indexOf(":") + 1);
                            h=19-Integer.parseInt(hTime);
                            m=59-Integer.parseInt(mTime);
                            limittime.setTime(h,m,59);
                            limittime.start();
                        }
                        else{
                            newGoodsAdapter=new NewGoodsAdapter(getContext(), promotionListBeen);
                            definiteList.setAdapter(newGoodsAdapter);
                            limit_out.setVisibility(View.GONE);
                            limit_ing.setVisibility(View.VISIBLE);

                            h=23-Integer.parseInt(hTime);
                            m=59-Integer.parseInt(mTime);
                            limittime.setTime(h,m,59);
                            limittime.start();
                            definiteList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Intent intent = new Intent(getContext(), CommidContentActivity.class);
                                    int id = promotionListBeen.get(i).getId();
                                    intent.putExtra("type","id");
                                    intent.putExtra("id", String.valueOf(id));
                                    startActivity(intent);
                                }
                            });
                        }

                    }
                }
            };
            OKUtilAPI.get(OKUtilAPI.HUOCommid + "?id=" + AppConfig.ID_FIVE, null, okUtilCallback);
        }
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_definite_goods;
    }


}
